package cn.ac.cintcm.service.impl;

import java.util.List;

import javax.persistence.EntityExistsException;

import org.springframework.dao.DataIntegrityViolationException;

import cn.ac.cintcm.dao.TherapyDao;
import cn.ac.cintcm.model.Therapy;
import cn.ac.cintcm.service.TherapyManager;

public class TherapyManagerImpl extends GenericManagerImpl<Therapy, Long> implements TherapyManager {

	private TherapyDao td;
	
	public TherapyManagerImpl(TherapyDao td) {
		super(td);
		this.td = td;
	}

	public List<Therapy> getDurationTherapy(long userId, String start, String end) {
		
		return td.getUserTherapy(userId, start, end);
	}

	public List<Therapy> getHistoryTherapy(long userId) {
		
		return td.getHistoryTherapy(userId);
	}

	public Therapy getLastTherapy(long userId) {
		
		return td.getLastTherapy(userId);
	}

	public List<Therapy> getTherapyThread(long userId, long parentId) {
		
		return td.getTherapyThread(userId, parentId);
	}

	public List<Therapy> getTherapy(Long uid) {
		
		return td.getTherapyByPatient(uid);
	}

	public Therapy getUnnotifiedTherapy(Long id) {
		
		return td.getUnnotifiedTherapyByPatient(id);
	}

	public List<Therapy> getDoctorTherapies(String username) {
		
		return td.getDoctorTherapies(username);
	}
	
	public Therapy save(Therapy t) {
		try {
			td.save(t);
	    } catch (DataIntegrityViolationException e) {
	        e.printStackTrace();
	        log.warn(e.getMessage());
	    } catch (EntityExistsException e) { 
	        e.printStackTrace();
	        log.warn(e.getMessage());
	    }
		return t;
	}

}
